package cn.org.timespace.plat.socket.mina.filter.command.concrete;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import cn.org.timespace.easyframe.util.StringUtils2;
import cn.org.timespace.plat.socket.mina.filter.ESPlatProtocalPack;
import cn.org.timespace.plat.socket.mina.filter.command.AbstractESPlatCommand;
import cn.org.timespace.plat.socket.mina.util.ProcessPlatPackage;
import cn.org.timespace.plat.vo.mc.PlatMconf;


/**
 * 信息终端上传配置信息
 * @author wind
 *
 */
public class UpMachinesConfCommand extends AbstractESPlatCommand {

	private final static Log log = LogFactory.getLog(UpMachinesConfCommand.class);
	private final static int numSta = 186;//信息机配置信息内容长度
	
	@Override
	public void processPackage(IoSession session, ESPlatProtocalPack pack) {
		ESPlatProtocalPack packReplay = ProcessPlatPackage.getPackReplay(pack);
		if(checkPackage(pack, ESPlatProtocalPack.SYNC_FRAME_NAME_UP)) {//消息正确
			//解析数据交换上传所承载的信息终端状态信息
			String contents = StringUtils2.convertNullToString(analyticalMsg(pack));
			if(!contents.equals("")){
				packReplay.setPackageContents(contents);
				packReplay.setCheckCodeAndPackageLength();
				log.debug("回复：" + packReplay.toString());
				session.write(packReplay.toString());
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				session.close(false);
				
				return;
			}
			
		}
			//出错
			packReplay.setPackageContents("ERROR");
			packReplay.setCheckCodeAndPackageLength();
			log.debug(packReplay.toString());
			session.write(packReplay.toString());
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			session.close(false);
		
	}
	
	/**
	 * 	数据交换状态信息
	 * @param pack
	 * @return
	 */
	public String analyticalMsg(ESPlatProtocalPack pack) {
		
		String packageContents = pack.getPackageContents();//包内容
		String result = "";
		
		try{
			int contentLength = packageContents.length();
			if(contentLength < numSta) {//如果包内容长度不是numSta
				log.debug("包内容长度不是numSta,解析失败");
				return null;
			}
			String mcId = pack.getMachineNo();
			PlatMconf platMconf = ProcessPlatPackage.getPlatMconf(mcId);
			if(platMconf != null){
				String status = platMconf.getStatus();
				if(status.equals("1")){
					result = status;
				}else{
					String startTime =ProcessPlatPackage.getStringTime(platMconf.getStartTime());//开始时间
					String endTime =ProcessPlatPackage.getStringTime(platMconf.getEndTime());//结束时间
					String mainHeadStatus = platMconf.getMainHeadStatus();//主读头状态
					String minorHeadStatus =platMconf.getMinorHeadStatus();//从读头状态
					String effectiveRate = ProcessPlatPackage.StrAddLength(3,platMconf.getEffectiveRate());//上卡率
					String ectStartTime =ProcessPlatPackage.getStringTime(platMconf.getEctStartTime());//开始时间
					String ectEndTime =ProcessPlatPackage.getStringTime(platMconf.getEctEndTime());//结束时间
					result=status+startTime+endTime+mainHeadStatus+minorHeadStatus+ectStartTime+ectEndTime+effectiveRate;
				}
			}else{
				result = "1";
			}
			return result;
		}
		catch(Exception e) {
			log.error("解析失败");
			log.debug(e.toString());
		}
		return null;
	}
	
}
